//
//  UserCenterViewController.m
//  JimiPay
//
//  Created by yull on 16/1/6.
//  Copyright (c) 2016年 com.shengyun. All rights reserved.
//

#import "UserCenterViewController.h"
#import "UpdatePasswordViewController.h"
#import "TDUserInfoViewController.h"
#import "TDUser.h"
#import "ExampleViewController.h"

@interface UserCenterViewController ()

@property (nonatomic, strong) NSArray *imageArray;
@property (nonatomic, strong) NSArray *titleArray;

@end

@implementation UserCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    self.navigationItem.title = [APPNAME isEqualToString:@"财有道"] ? @"认证中心" : @"个人中心";
    [self setupShareRightItem];
    [self setupValidateLeftItem:[[TDUser defaultUser].custStatus isEqualToString:@"2"]];
    
    self.imageArray = @[@"usercenter_geren_bank",
                        @"usercenter_lock"];
    self.titleArray = @[@"储蓄卡变更",
                        @"密码修改"];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UserCenterCell"];
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    [TDHttpEngine requestForGetCustInfWithCustMobile:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_MOBILE] custId:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_ID] complete:^(BOOL succeed, NSString *msg, NSString *cod, TDUser *user) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (succeed) {
            self.userNameL.text = user.custName.length ? user.custName : @"***";
            self.userPhoneL.text = [[NSUserDefaults standardUserDefaults] objectForKey:CUST_MOBILE];
            
            if ([[TDUser defaultUser].cardBundingStatus isEqualToString:@"1"]) {
                
                self.cardAuthenticationL.text = @"审核中";
            } else if ([[TDUser defaultUser].cardBundingStatus isEqualToString:@"2"]) {

                self.cardAuthenticationL.text = @"已绑定";
            } else {
                
                self.cardAuthenticationL.text = @"未绑定";
            }
            
            if ([[TDUser defaultUser].custStatus isEqualToString:@"1"]) {
                
                self.IDAuthenticationL.text = @"审核中";
            } else if ([[TDUser defaultUser].custStatus isEqualToString:@"2"]) {
                
                self.IDAuthenticationL.text = @"已认证";
            } else {
                
                self.IDAuthenticationL.text = @"未认证";
            }
        }
        else
        {
            [self.view makeToast:msg duration:2.0f position:@"center"];
        }
    }];
}

- (void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    
    if ([[TDUser defaultUser].cardBundingStatus isEqualToString:@"1"]) {
        
        self.cardAuthenticationL.text = @"审核中";
    } else if ([[TDUser defaultUser].cardBundingStatus isEqualToString:@"2"]) {
        
        self.cardAuthenticationL.text = @"已绑定";
    } else {
        
        self.cardAuthenticationL.text = @"未绑定";
    }
    
    if ([[TDUser defaultUser].custStatus isEqualToString:@"1"]) {
        
        self.IDAuthenticationL.text = @"审核中";
    } else if ([[TDUser defaultUser].custStatus isEqualToString:@"2"]) {
        
        self.IDAuthenticationL.text = @"已认证";
    } else {
        
        self.IDAuthenticationL.text = @"未认证";
    }
}

- (IBAction)button_IDAuthentication:(id)sender {
    
    if ([[TDUser defaultUser].custStatus isEqualToString:@"1"]) {
        [self.view makeToast:@"实名认证正在审核中" duration:2.0f position:@"center"];
        return;
    } else if (![[TDUser defaultUser].custStatus isEqualToString:@"2"]) {
        
        NSString *key = [NSString stringWithFormat:@"cer-%@", [[NSUserDefaults standardUserDefaults] objectForKey:CUST_ID]];
        NSNumber *isCer = [[NSUserDefaults standardUserDefaults] objectForKey:key];
        
        ExampleViewController *controller = [[ExampleViewController alloc] init];
        controller.hidesBottomBarWhenPushed = YES;
        
        if (![[TDUser defaultUser].cardBundingStatus isEqualToString:@"2"] &&
            ![[TDUser defaultUser].cardBundingStatus isEqualToString:@"1"] && 
            !isCer && !isCer.boolValue) {
            
            controller.isAll = YES;
        } else {
            
            controller.isRealName = YES;
        }
        
        [self.navigationController pushViewController:controller animated:YES];
    }
}

- (IBAction)button_cardAuthentication:(id)sender {
    
    if ([[TDUser defaultUser].cardBundingStatus isEqualToString:@"1"]) {
        [self.view makeToast:@"绑定银行卡正在审核中" duration:2.0f position:@"center"];
        return;
    } else if (![[TDUser defaultUser].cardBundingStatus isEqualToString:@"2"]) {
        
        NSString *key = [NSString stringWithFormat:@"cer-%@", [[NSUserDefaults standardUserDefaults] objectForKey:CUST_ID]];
        NSNumber *isCer = [[NSUserDefaults standardUserDefaults] objectForKey:key];
        
        ExampleViewController *controller = [[ExampleViewController alloc] init];
        controller.hidesBottomBarWhenPushed = YES;
        
        if (![[TDUser defaultUser].custStatus isEqualToString:@"2"] &&
            ![[TDUser defaultUser].custStatus isEqualToString:@"1"] &&
            !isCer && !isCer.boolValue) {
            
            controller.isAll = YES;
        } else {
            
            controller.isBindCard = YES;
        }
        
        [self.navigationController pushViewController:controller animated:YES];
        
    } else {
        TDUserInfoViewController *controller = [[TDUserInfoViewController alloc] init];
        controller.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:controller animated:YES];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.imageArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UserCenterCell"];
    
    cell.imageView.image = [UIImage imageNamed:self.imageArray[indexPath.row]];
    cell.textLabel.text = self.titleArray[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 0) {
        if ([[TDUser defaultUser].cardBundingStatus isEqualToString:@"1"]) {
            [self.view makeToast:@"绑定银行卡正在审核中" duration:2.0f position:@"center"];
            return;
        } else if (![[TDUser defaultUser].cardBundingStatus isEqualToString:@"2"]) {
            [self.view makeToast:@"还未绑定银行卡" duration:2.0f position:@"center"];
            return;
        }
        
        if ([[TDUser defaultUser].custStatus isEqualToString:@"1"]) {
            [self.view makeToast:@"实名认证正在审核中" duration:2.0f position:@"center"];
            return;
        } else if (![[TDUser defaultUser].custStatus isEqualToString:@"2"]) {
            [self.view makeToast:@"您尚未实名认证，请先完善资料！" duration:2.0f position:@"center"];
            return;
        }
        
        ExampleViewController *controller = [[ExampleViewController alloc] init];
        controller.hidesBottomBarWhenPushed = YES;
        controller.isBindCard = YES;
        [self.navigationController pushViewController:controller animated:YES];
        
    } else {
        UpdatePasswordViewController *controller = [[UpdatePasswordViewController alloc] init];
        controller.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:controller animated:YES];
    }
}

@end
